{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "# 创建型模式\n",
    "class MyClass:\n",
    "    # 类属性，用于多实例共享一个属性\n",
    "    _class_variable = None\n",
    "\n",
    "    def __init__(self, var):\n",
    "        # 实例属性\n",
    "        self.get_variable = var\n",
    "    \n",
    "    @property\n",
    "    def get_variable(self):\n",
    "        return self._class_variable\n",
    "    \n",
    "    # 描述器\n",
    "    @get_variable.setter\n",
    "    def get_variable(self, var):\n",
    "        self.__class__._class_variable = var\n",
    "    \n",
    "    def display(self):\n",
    "        return self.get_variable\n",
    "\n",
    "c1 = MyClass(1)\n",
    "c2 = MyClass(2)\n",
    "c2.get_variable = 3\n",
    "\n",
    "\n",
    "print(c1.get_variable)\n",
    "print(c1.display())\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.7 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
